const Parse = require('parse/node');
const _=require('lodash');
const BorrowTimeObj = Parse.Object.extend("AutoBorrow_BorrowTime");
const ParseHelper = require('../helpers/parse');
const moment=require('moment');
const KindService=require("../services/kind")

//创建名单
async function add(schedule){
    const borrowTimeObj=new BorrowTimeObj(schedule);
    await borrowTimeObj.save(null,{ useMasterKey: true });
    return ParseHelper.toJSON(borrowTimeObj);
}

//删除
async function remove(scheduleId){
    const borrowTimeObj=BorrowTimeObj.createWithoutData(scheduleId);
    borrowTimeObj.set("isDeleted",true);
    await borrowTimeObj.save(null,{ useMasterKey: true });
    return ParseHelper.toJSON(borrowTimeObj);
}


async  function getScheduleDays(){
    const query = new Parse.Query(BorrowTimeObj);
    query.equalTo('name',name);
    query.notEqualTo('isDeleted',true);
    return  query.first({ useMasterKey: true }).then(ParseHelper.toJSON);
}




async function update(scheduleId,schedule){
    const borrowTimeObj= BorrowTimeObj.createWithoutData(scheduleId);
    borrowTimeObj.set('date',schedule.date);
    borrowTimeObj.set('begin',schedule.begin);
    borrowTimeObj.set('end',schedule.end);
    borrowTimeObj.set('capacity',schedule.capacity);
    await borrowTimeObj.save(null,{ useMasterKey: true });
    return ParseHelper.toJSON(borrowTimeObj);
}

async function getSchedules(){
    const today=moment().format('YYYY-MM-DD');
    const time=moment().format('HH:mm')
    const query = new Parse.Query(BorrowTimeObj);
    query.greaterThanOrEqualTo("date", today);
    // query.greaterThan("begin", time);
    query.notEqualTo('isDeleted',true);
    query.ascending("date")
    query.limit(4)
    const _schedules=await query.find({ useMasterKey: true }).then(ParseHelper.toJSONList);
    const schedules=_schedules.filter(s=>s.date===today&&s.end>time||s.date>today)
    const objs= schedules.map(async schedule=>{
        const apply_count=schedule.users?schedule.users.length:0
        return{
            ...schedule,
            apply_count,
            label:schedule.date+" "+schedule.begin+"~"+schedule.end+"  "+apply_count+"/"+schedule.capacity,
            value:schedule.objectId,
        }
    });
    return await Promise.all(objs)
}

async function addUser(id,username){
    const borrowTimeObj= BorrowTimeObj.createWithoutData(id);
    borrowTimeObj.addUnique('users', username);
    await borrowTimeObj.save(null,{ useMasterKey: true });
    return ParseHelper.toJSON(borrowTimeObj);
}

async function getSchedule(id){
    const query = new Parse.Query(BorrowTimeObj);
    return query.get(id,{ useMasterKey: true }).then(ParseHelper.toJSON)
}

async function getSchedulesByIds(ids){
        const query = new Parse.Query(BorrowTimeObj);
        query.notEqualTo('isDeleted', true);
        query.containedIn('objectId',ids);
        query.limit(9999);
        return query.find({ useMasterKey: true }).then(ParseHelper.toJSONList);
}

module.exports={
    add,
    remove,
    update,
    getSchedules,
    addUser,
    getSchedule,
    getSchedulesByIds
};